package pro.lepers.portfolio.view.misc {
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.utils.Dictionary;

	/**
	 * @author plepers
	 */
	public class OverLayer extends Sprite {
		
		public function OverLayer () {
			if( instance != null ) throw new Error( "pro.lepers.portfolio.view.misc.OverLayer est deja instancié" );
			
			_dChildsDatas = new Dictionary();
		}

		
		public function retreiveChild (child : DisplayObject) : void {
			var dlData : DisplayListData = _dChildsDatas[ child ];
			if( dlData == null ) return;
			delete _dChildsDatas[ child ];
			
			child.x = dlData.x;
			child.y = dlData.y;
			
			dlData.parent.addChildAt( child, dlData.depth );
			
			dlData.dispose();
		}

		override public function addChild (child : DisplayObject) : DisplayObject {
			
			var dlData : DisplayListData = new DisplayListData( child );
			_dChildsDatas[ child ] = dlData;
			
			var p : Point = child.localToGlobal( NULL_POINT );
			child.x = p.x;
			child.y = p.y;
			
			return super.addChild( child );
		}

		private var _dChildsDatas : Dictionary;




		public static var instance : OverLayer;
		
		
		public static function start(  ) : OverLayer {
			if (instance == null)
				instance = new OverLayer();
			return instance;
		}
		
		private static const NULL_POINT : Point = new Point();
	}
}

import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;

class DisplayListData {
	
	public function DisplayListData ( child : DisplayObject ) {
		x = child.x;
		y = child.y;
		parent = child.parent;
		depth = parent.getChildIndex(child );
	}

	public function dispose () : void {
		parent = null;
	}

	
	
	public var x : Number;
	public var y : Number;
	public var parent : DisplayObjectContainer;
	public var depth : uint;
	
}
